package com.jack.blog.controller;

import com.jack.blog.cache.NoticeServiceCache;
import com.jack.blog.service.UserService;
import com.jack.blog.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user/{uid}")
public class NoticeController {

    @Autowired
    private NoticeServiceCache noticeServiceCache;

    @PostMapping("/notice")
    public String addNotice(@PathVariable("uid")int uid, NoticeVo noticeVo, Model model){
        String url="/user/"+uid+"/notices";
        try {
            noticeServiceCache.addNotice(uid,noticeVo);
            return "redirect:"+url;
        }catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "add error!");
            model.addAttribute("backUrl",url);
            return  "error";
        }
    }

    @GetMapping("/notice/{nid}/delete")
    public String deleteNotice(@PathVariable("uid")int uid, @PathVariable("nid") int nid, Model model){
        String url="/user/"+uid+"/notices";
        try {
            noticeServiceCache.deleteNotice(uid,nid);
            return "redirect:"+url;
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error", "delete error!");
            model.addAttribute("backUrl",url);
            return "error";
        }
    }

    @GetMapping("/notice/{nid}/top")
    public String topNotice(@PathVariable("uid")int uid, @PathVariable("nid") int nid, Model model){
        String url="/user/"+uid+"/notices";
        try {
            noticeServiceCache.topNotice(uid,nid);
            return "redirect:"+url;
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error", "top error!");
            model.addAttribute("backUrl",url);
            return "error";
        }
    }

    @GetMapping("/notice/{nid}/unTop")
    public String unTopNotice(@PathVariable("uid")int uid, @PathVariable("nid") int nid, Model model){
        String url="/user/"+uid+"/notices";
        try {
            noticeServiceCache.unTopNotice(uid,nid);
            return "redirect:"+url;
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error", "top error!");
            model.addAttribute("backUrl",url);
            return "error";
        }
    }
}
